Proxy bun客户端的代理
DB*bun.DBcallBacks[]CallbackDB.DB.cleanerChchan struct{}DB.DB.closedboolDB.DB.connRequestsmap[uint64]chan sql.connRequestDB.DB.connectordriver.ConnectorDB.DB.depmap[sql.finalCloser]sql.depSetDB.DB.freeConn[]*sql.driverConn
// stacktrace of last conn's put; debug only
// Total number of connections closed due to idle count.
// zero means defaultMaxIdleConns; negative means 0
// maximum amount of time a connection may be idle before being closed
// Total number of connections closed due to idle time.
// maximum amount of time a connection may be reused
// Total number of connections closed due to max connection lifetime limit.
// <= 0 means unlimited
// protects following fields
// Next key to use in connRequests.
numClosed is an atomic counter which represents a total number of
closed connections. Stmt.openStmt checks it before cleaning closed
connections in Stmt.css.
// number of opened and pending open connections
Used to signal the need for new connections
a goroutine running connectionOpener() reads on this chan and
maybeOpenNewConnections sends on the chan (one send per needed connection)
It is closed during db.Close(). The close tells the connectionOpener
goroutine to exit.
// stop cancels the connection opener.
// Total number of connections waited for.
Atomic access only. At top of struct to prevent mis-alignment
on 32-bit platforms. Of type time.Duration.
// Total time waited for new connections.
DB.dialectschema.DialectDB.featuresfeature.FeatureDB.flagsinternal.FlagDB.fmterschema.FormatterDB.queryHooks[]bun.QueryHookDB.statsbun.DBStatsqueryTimeouttime.Duration( T) AddQueryHook(hook bun.QueryHook)( T) Begin() (bun.Tx, error)( T) BeginTx(ctx context.Context, opts *sql.TxOptions) (bun.Tx, error)
Close closes the database and prevents new queries from starting.
Close then waits for all queries that have started processing on the server
to finish.
It is rare to Close a DB, as the DB handle is meant to be
long-lived and shared between many goroutines.
( T) Conn(ctx context.Context) (bun.Conn, error)( T) DBStats() bun.DBStats( T) Dialect() schema.Dialect
Driver returns the database's underlying driver.
( T) Exec(query string, args ...interface{}) (sql.Result, error)( T) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)( T) Formatter() schema.Formatter
Init 初始化代理对象
IsOk 检查代理是否已经可用
( T) NewAddColumn() *bun.AddColumnQuery( T) NewCreateIndex() *bun.CreateIndexQuery( T) NewCreateTable() *bun.CreateTableQuery
NewCtx 根据注册的超时时间构造一个上下文
( T) NewDelete() *bun.DeleteQuery( T) NewDropColumn() *bun.DropColumnQuery( T) NewDropIndex() *bun.DropIndexQuery( T) NewDropTable() *bun.DropTableQuery( T) NewInsert() *bun.InsertQuery( T) NewSelect() *bun.SelectQuery( T) NewTruncateTable() *bun.TruncateTableQuery( T) NewUpdate() *bun.UpdateQuery( T) NewValues(model interface{}) *bun.ValuesQuery
Ping verifies a connection to the database is still alive,
establishing a connection if necessary.
PingContext verifies a connection to the database is still alive,
establishing a connection if necessary.
( T) Prepare(query string) (bun.Stmt, error)( T) PrepareContext(ctx context.Context, query string) (bun.Stmt, error)( T) Query(query string, args ...interface{}) (*sql.Rows, error)( T) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)( T) QueryRow(query string, args ...interface{}) *sql.Row( T) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
Regist 注册回调函数,在init执行后执行回调函数
如果对象已经设置了被代理客户端则无法再注册回调函数
( T) RegisterModel(models ...interface{})( T) ResetModel(ctx context.Context, models ...interface{}) error
RunInTx runs the function in a transaction. If the function returns an error,
the transaction is rolled back. Otherwise, the transaction is committed.
( T) ScanRow(ctx context.Context, rows *sql.Rows, dest ...interface{}) error( T) ScanRows(ctx context.Context, rows *sql.Rows, dest ...interface{}) error
SetConnMaxIdleTime sets the maximum amount of time a connection may be idle.
Expired connections may be closed lazily before reuse.
If d <= 0, connections are not closed due to a connection's idle time.
SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
Expired connections may be closed lazily before reuse.
If d <= 0, connections are not closed due to a connection's age.
SetConnect 设置连接的客户端
@params cli *bun.DB bun的DB对象
SetMaxIdleConns sets the maximum number of connections in the idle
connection pool.
If MaxOpenConns is greater than 0 but less than the new MaxIdleConns,
then the new MaxIdleConns will be reduced to match the MaxOpenConns limit.
If n <= 0, no idle connections are retained.
The default max idle connections is currently 2. This may change in
a future release.
SetMaxOpenConns sets the maximum number of open connections to the database.
If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than
MaxIdleConns, then MaxIdleConns will be reduced to match the new
MaxOpenConns limit.
If n <= 0, then there is no limit on the number of open connections.
The default is 0 (unlimited).
SetQueryTimeout 设置连接的请求超时
@params timeout time.Duration
Stats returns database statistics.
( T) String() string( T) Table(typ reflect.Type) *schema.Table( T) WithNamedArg(name string, value interface{}) *bun.DB
addDep notes that x now depends on dep, and x's finalClose won't be
called until all of x's dependencies are removed with removeDep.
( T) addDepLocked(x sql.finalCloser, dep interface{})( T) afterQuery(ctx context.Context, event *bun.QueryEvent, res sql.Result, err error)( T) afterQueryFromIndex(ctx context.Context, event *bun.QueryEvent, hookIndex int)( T) beforeQuery(ctx context.Context, queryApp schema.Query, query string, queryArgs []interface{}) (context.Context, *bun.QueryEvent)( T) begin(ctx context.Context, opts *sql.TxOptions, strategy sql.connReuseStrategy) (tx *sql.Tx, err error)
beginDC starts a transaction. The provided dc must be valid and ready to use.
( T) clone() *bun.DB
conn returns a newly-opened or cached *driverConn.
( T) connectionCleaner(d time.Duration)( T) connectionCleanerRunLocked() (closing []*sql.driverConn)
Runs in a separate goroutine, opens new connections when requested.
( T) exec(ctx context.Context, query string, args []interface{}, strategy sql.connReuseStrategy) (sql.Result, error)( T) execDC(ctx context.Context, dc *sql.driverConn, release func(error), query string, args []interface{}) (res sql.Result, err error)( T) format(query string, args []interface{}) string( T) makeQueryBytes() []byte( T) maxIdleConnsLocked() int
Assumes db.mu is locked.
If there are connRequests and the connection limit hasn't been reached,
then tell the connectionOpener to open new connections.
nextRequestKeyLocked returns the next connection request key.
It is assumed that nextRequest will not overflow.
noteUnusedDriverStatement notes that ds is no longer used and should
be closed whenever possible (when c is next not in use), unless c is
already closed.
Open one new connection
( T) pingDC(ctx context.Context, dc *sql.driverConn, release func(error)) error( T) prepare(ctx context.Context, query string, strategy sql.connReuseStrategy) (*sql.Stmt, error)
prepareDC prepares a query on the driverConn and calls release before
returning. When cg == nil it implies that a connection pool is used, and
when cg != nil only a single driver connection is used.
putConn adds a connection to the db's free pool.
err is optionally the last error that occurred on this connection.
Satisfy a connRequest or put the driverConn in the idle pool and return true
or return false.
putConnDBLocked will satisfy a connRequest if there is one, or it will
return the *driverConn to the freeConn list if err == nil and the idle
connection limit will not be exceeded.
If err != nil, the value of dc is ignored.
If err == nil, then dc must not equal nil.
If a connRequest was fulfilled or the *driverConn was placed in the
freeConn list, then true is returned, otherwise false is returned.
( T) query(ctx context.Context, query string, args []interface{}, strategy sql.connReuseStrategy) (*sql.Rows, error)
queryDC executes a query on the given connection.
The connection gets released by the releaseConn function.
The ctx context is from a query method and the txctx context is from an
optional transaction context.
removeDep notes that x no longer depends on dep.
If x still has dependencies, nil is returned.
If x no longer has any dependencies, its finalClose method will be
called and its error value will be returned.
( T) removeDepLocked(x sql.finalCloser, dep interface{}) func() error( T) shortestIdleTimeLocked() time.Duration
startCleanerLocked starts connectionCleaner if needed.
T : github.com/uptrace/bun.IConn
T : github.com/uptrace/bun.IDB
T : fmt.Stringer
T : io.Closer
T : context.stringer
T : os/signal.stringer
T : runtime.stringer
func New() *Proxy
var DB *Proxy
The pages are generated with Goldsv0.3.6. (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.